/* Copyright (c) 2012 Ronan Mac an tSaoir

	 Licensed under the Apache License, Version 2.0 (the "License");
	 you may not use this file except in compliance with the License.
	 You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

	 Unless required by applicable law or agreed to in writing, software
	 distributed under the License is distributed on an "AS IS" BASIS,
	 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	 See the License for the specific language governing permissions and
	 limitations under the License.
*/

package com.enjoyireland.hiking;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

/**
 * Welcome Page
 * 	From here:
 * 		- Find a walk
 * 		- Load recently viewed walks
 * 
 *  Context menu:
 *  	- About this app
 *  
 * @author Rónan Mac an tSaoir
 */
public class HikeIrelandActivity extends ListActivity implements Constants {

	GoogleAnalyticsTracker tracker;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        tracker = GoogleAnalyticsTracker.getInstance();

        // Start the tracker in manual dispatch mode...
        tracker.startNewSession("UA-31162696-1", this);
        
        /* load values for welcome page */
        final String [] links = getResources().getStringArray(R.array.welcomePage);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.main, links));
        ListView lv = getListView();
        lv.setFooterDividersEnabled(false); 
        lv.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
        lv.setId(android.R.id.list);

        /* onClick, load relevant View */
  	  	lv.setOnItemClickListener(new OnItemClickListener() {
  	  		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  	  			/* load appropriate activity */
	  	  		Intent find = new Intent(getApplicationContext(),ChooseWalk.class);
	  	  		switch( Long.valueOf(id).intValue() ){
	  	  		case 0: startActivity(find);
	  	  		}
  	  		}
  	  	});      
    }
    
    /* create context menu */
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		// add button for about text
		menu.add(0, MENU_ABOUT, Menu.NONE, R.string.about).setIcon(android.R.drawable.ic_menu_info_details);
		menu.add(0, INSTRUCTIONS, Menu.NONE, R.string.info).setIcon(android.R.drawable.ic_menu_compass);

		return true;
	}
	
	/* show about dialog */
	@Override
	public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
		if (item.getTitle().equals(getResources().getString(R.string.about)))
			showDialog(ABOUT_DIALOG);
		else
			showDialog(INSTRUCTIONS);
		return true;
	}
	
	/* create about dialog, with html format text and ok button */
	@Override
	protected Dialog onCreateDialog(final int id) {
		Dialog dialog;

		switch (id) {
		case ABOUT_DIALOG:
			return new AlertDialog.Builder(HikeIrelandActivity.this).setIcon(R.drawable.icon)
					.setTitle(R.string.app_name).setMessage(Html.fromHtml(getResources().getString(R.string.aboutText)))
					.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(final DialogInterface dialog, final int whichButton) {
						}
					}).create();
		case INSTRUCTIONS:
			return new AlertDialog.Builder(HikeIrelandActivity.this).setIcon(R.drawable.icon)
					.setTitle(R.string.app_name).setMessage(Html.fromHtml(getResources().getString(R.string.instructions)))
					.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(final DialogInterface dialog, final int whichButton) {
						}
					}).create();
		default:
			dialog = null;
			break;
		}
		return dialog;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// Stop the tracker when it is no longer needed.
		tracker.stopSession();
	}
}